$(function () {
    var isFirst = false;
    //加载公司机构树
    $("#yg_gsjg_tree").tree({
        loadFilter:function (rows) {
            return convert(rows);
        },
        onClick:function (node) {
            searchYg();
        },
        onLoadSuccess:function (node) {
            if (isFirst) {
                setTimeout(function () {
                    searchYg();
                }, 300)
            } else {
                isFirst = true;
            }
        }
    });

    //员工表格
    $("#ygDataGrid").datagrid({
        onLoadSuccess:function (data) {
            $("#yg_mod").hide();
            $("#yg_del").hide();
            $("#yg_reset").hide();
            $("#ygbmsz").hide();
        },
        rowStyler:function (index, row) {
            if (row.kdl == '否') {
                return 'background-color:rgba(255, 130, 87, 0.64);';
            }
        },
        onDblClickCell:function (index, field, value) {
            if (field == "xm") {
                if ($("#yg_mod")) {
                    $("#yg_mod").click();
                }
            }
        },
        onSelect:function (rowIndex, rowData) {
            $("#yg_mod").show();
            $("#yg_del").show();
            $("#yg_reset").show();
            $("#ygbmsz").show();
            if (rowData.kdl == null || rowData.kdl == '是') {
                $("#yg_start").text('停用');
                $("#yg_del").hide();
                $("#yg_start").removeClass('start').addClass('stop');
                $('#yg_start').unbind('click');
                $('#yg_start').bind('click', function () {
                    changeYgState($.webPath() + '/sys/yg/stop_yg', '确认停用此员工？');
                });
            } else {
                $("#yg_start").text('启用');

                $("#yg_start").removeClass('stop').addClass('start');
                $('#yg_start').unbind('click');
                $('#yg_start').bind('click', function () {
                    changeYgState($.webPath() + '/sys/yg/start_yg', '确认启用此员工？');
                });
            }
            $("#yg_start").show();
        }
    });
    //=============================员工页面的按钮触发事件================================================
    //新增员工
    $("#yg_new").click(function () {
        addYgDialog("新增员工", "add_yg");
    });
    //修改员工
    $("#yg_mod").click(function () {
        modifyYgDialog('修改员工', 'update_yg');
    });
    //删除员工
    $("#yg_del").click(function () {
        deleteYg();
    });
    //重置密码
    $("#yg_reset").click(function () {
        reset();
    });
    //搜索员工
    $("#yg_search_btn").click(function () {
        searchYg();
    });
    //编码范围设置
    $("#ygbmsz").click(function(){
        var ygid = $("#ygDataGrid").datagrid("getSelected").id;
        if (ygid) {
            ygbmfwwin = $.window({
                title:'设置编码范围',
                width:750,
                height:450,
                cache:false,
                url:$.webPath() + '/sys/yg/ygbmfwsz?ygId=' + ygid,
                modal:true,
                buttons:[
                    {
                        text:'保存',
                        handler:function(){
                            $("#ygbm-save").click();
                        }
                    }
                ]
            });
        }
    });
    //员工电话号码导出
    $("#yg_export").click(function () {
        var isSelected = !!$("#yg_gsjg_tree").tree("getSelected");
        var selectedNodeID;
        if (isSelected) {
            if ($("#yg_gsjg_tree").tree("getSelected").attributes &&
                $("#yg_gsjg_tree").tree("getSelected").attributes.parentId == null) {
                selectedNodeID = "";
            } else {
                selectedNodeID = $("#yg_gsjg_tree").tree("getSelected").id;
            }

        } else {
            if ($("#yg_gsjg_tree").tree("getRoot").attributes &&
                $("#yg_gsjg_tree").tree("getRoot").attributes.parentId == null) {
                selectedNodeID = "";
            } else {
                selectedNodeID = $("#yg_gsjg_tree").tree("getRoot").id;
            }
        }
        window.open($.webPath() + "/sys/yg/yg_export?zgjgId=" + selectedNodeID);
    });

    //员工电话号码导入
    $("#yg_import").click(function(){
        var win = $.window({
            title:"员工电话号码信息导入",
            width:450,
            height:240,
            cache:false,
            url:$.webPath() + "/sys/yg/yg_upload",
            modal:true ,
            buttons:[
                {
                    text:'确认',
                    handler:function () {
                        $('#uploadSubject').form('submit', {
                            onSubmit:function(){
                                $(win).mask();
                            } ,
                            success:function (data) {
                                $(win).mask('hide');
                                var json = $.parseJSON(data);
                                if (json.statusCode == 200) {
                                    $(win).window('close');
                                    $('#ygDataGrid').datagrid('reload');
                                    $.messager.show({
                                        title:'提示',
                                        msg:json.message,
                                        timeout:3000,
                                        showType:'slide'
                                    });
                                } else
                                    $.messager.alert('提示', json.message, 'error');
                            }
                        });
                    }
                }
            ]
        });
    });
});
//查询按钮绑定的查询函数
function searchYg() {
    var isSelected = !!$("#yg_gsjg_tree").tree("getSelected");
    var selectedNodeID;
    if (isSelected) {
        if ($("#yg_gsjg_tree").tree("getSelected").attributes &&
            $("#yg_gsjg_tree").tree("getSelected").attributes.parentId == null) {
            selectedNodeID = "";
        } else {
            selectedNodeID = $("#yg_gsjg_tree").tree("getSelected").id;
        }

    } else {
        if ($("#yg_gsjg_tree").tree("getRoot").attributes &&
            $("#yg_gsjg_tree").tree("getRoot").attributes.parentId == null) {
            selectedNodeID = "";
        } else {
            selectedNodeID = $("#yg_gsjg_tree").tree("getRoot").id;
        }
    }

    $('#ygDataGrid').datagrid({
        url:$.webPath() + "/sys/yg/ygList",
        pageNumber:1,
        queryParams:{
            gh:$('#yggh').val(),
            xm:$('#ygxm').val(),
            rzsjStart:$('#rzsjStart').datebox('getValue'),
            rzsjEnd:$('#rzsjEnd').datebox('getValue'),
            zgjgId:selectedNodeID}
    });
}

function showYgDialog(title, href, actionUrl) {
    var win = $.window({
        onBeforeClose:function () {
            $(".validatebox-tip").remove();
        },
        title:title,
        width:800,
        height:400,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'提交',
                handler:function () {
//                    $.messager.progress();
                    $(win).mask();
                    $("#ygForm").form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
//                                $.messager.progress('close');
                                $(win).mask('hide');
                                $("#ygForm .tabs-title :eq(0)").click();
                            }
                            return isValid;
                        },
                        success:function (data) {
//                            $.messager.progress('close');
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                searchYg();
                                $.messager.show({
                                    title:'提示框',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else {
                                $.messager.alert('提示', json.message, 'error');
                            }
                        }
                    });
                }
            }
        ]
    });
}

//密码重置
function reset() {
    var row = $('#ygDataGrid').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否重置密码？', function (r) {
            if (r) {
                var index = $('#ygDataGrid').datagrid('getRowIndex', row);
                $.ajax({
                    url:$.webPath() + "/sys/yg/reset_mm",
                    dataType:'text',
                    type:'POST',
                    data:{ygId:row.id},
                    beforeSend:function () {
                        $("#yg_layout").mask();
                    },
                    success:function (data) {
                        $("#yg_layout").mask("hide");
                        if (data == '重置密码成功') {
                            $.messager.show({
                                title:'提示',
                                msg:data,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else
                            $.messager.alert('提示', data, 'error');
                    }
                });
            }
        });
    }
}

//弹出新增员工页面
function addYgDialog(title, action) {
    var zgjgId = $("#yg_gsjg_tree").tree("getSelected") == null ? "" : $("#yg_gsjg_tree").tree("getSelected").id;
    var href = $.webPath() + "/sys/yg/yg_content?zgjgId=" + zgjgId;
    var actionUrl = $.webPath() + "/sys/yg/" + action;
    showYgDialog(title, href, actionUrl);
}


//弹出修改员工页面
function modifyYgDialog(title, action) {
    var record = $("#ygDataGrid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录!", "info");
        return;
    }
    var href = $.webPath() + "/sys/yg/yg_content?ygId=" + record.id;
    var actionUrl = $.webPath() + "/sys/yg/" + action;
    showYgDialog(title, href, actionUrl);
}
//删除员工的方法
function deleteYg() {
    var record = $("#ygDataGrid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要删除的记录!", "info");
        return;
    }
    $.messager.confirm("提示", "是否确认删除该人员?", function (r) {
        if (r) {
            $.ajax({
                type:"POST",
                url:$.webPath() + "/sys/yg/delete_yg",
                dataType:"json",
                data:{ygId:record.id},
                beforeSend:function () {
//                    $.messager.progress();
                    $("#yg_layout").mask();
                },
                success:function (data) {
//                    $.messager.progress("close");
                    $("#yg_layout").mask('hide');
                    var icon = "info";
                    if (data.statusCode == 300)
                        icon = "error";
                    $.messager.alert("提示", data.message, icon);
                    if (data.statusCode == 200)
                        searchYg();
                }
            });
        }
    });
}

//改变状态，启用停用
function changeYgState(url, message) {
    var ygId = $("#ygDataGrid").datagrid("getSelected").id;
    if (ygId) {
        $.messager.confirm("确认", message, function (r) {
            if (r) {
                $.ajax({
                    url:url + "?ygId=" + ygId,
                    type:"json",
                    success:function (data) {
                        if (data.statusCode == '200') {
                            $('#ygDataGrid').datagrid('reload');
//                            $("#yg_gsjg_tree").tree("reload");
//                            $("#parent_yg").combotree("reload", $.webPath() + "/organ/gsjg/allgsjg_tree");
                            $.messager.show({
                                title:'提示框',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                            $("#yg_start").hide();
                        } else {
                            $.messager.alert('提示', data.message, 'info');
                        }
                    }
                });
            }
        })
    }
}